package vcp.client.GUI.mvc.model;

import java.util.ArrayList;
import java.util.List;

import vcp.client.forms.ApproveCostForm;
import vcp.client.interfaces.IObservable;
import vcp.client.request.SiteCostApproveRequest;
import vcp.entity.SiteCostUpdates;
import vcp.entity.Worker;
/**
 * 
 * @author VCP team
 *<p>
 *approve coast model
 *</p>
 *@see UIAbstractModel
 */
public class UIApproveCostsModel extends UIAbstractModel
{
	/** CEO instance **/
	private Worker CEO;

	public Worker getCEO ()
	{
		return this.CEO;
	}

	public void setCEO ( Worker cEO )
	{
		this.CEO = cEO;
	}

	public UIApproveCostsModel ( IObservable model )
	{
		super(model);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * 
	 * @return list with coast updates
	 */
	public List <SiteCostUpdates> getPendingSiteCostsUpdates ()
	{
		return this.CEO.getSiteCostUpdates();
	}
	/**
	 * approve the new coasts
	 * @param approveCostForm
	 */
	public void applyApprovalCosts ( ApproveCostForm approveCostForm )
	{
		SiteCostApproveRequest request = new SiteCostApproveRequest(approveCostForm);
		this.sendRequest(request, true);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * remove the site with new coast updates
	 * @param removeList
	 */
	private void removedApprovedSiteCostsUpdates ( ArrayList <SiteCostUpdates> removeList )
	{
		ArrayList <Integer> indexList = new ArrayList <>();

		for ( int i = 0; i < this.CEO.getSiteCostUpdates().size(); i ++ )
		{
			for ( SiteCostUpdates siteCostUpdates : removeList )
			{
				if ( this.CEO.getSiteCostUpdates().get(i).getSiteEnum() == siteCostUpdates.getSiteEnum() )
				{
					indexList.add(i);
				}
			}
		}

		for ( Integer integer : indexList )
		{
			this.CEO.getSiteCostUpdates().remove(integer.intValue());
		}
	}
	/**
	 * add new approval request to the list of requests
	 * @param newSiteCostUpdate
	 */
	private void addToBeApprovedSiteCostsUpdates ( SiteCostUpdates newSiteCostUpdate )
	{
		this.CEO.getSiteCostUpdates().add(newSiteCostUpdate);
	}

	/****************************************************************************************************************************
	 *** UIAbstractModel methods ***
	 ****************************************************************************************************************************/

	@Override
	public void clearModel ()
	{
		this.CEO = null;
	}

	@Override
	public void shouldRegisterForUpdates ()
	{
		this.model.registerForUpdates(this);
	}

	/****************************************************************************************************************************
	 *** Observer methods ***
	 ****************************************************************************************************************************/
	@SuppressWarnings ( "unchecked" )
	@Override
	public void update ( Object data )
	{
		if ( data instanceof ArrayList <?> )
		{
			ArrayList <SiteCostUpdates> listToFilter = (ArrayList <SiteCostUpdates>) data;
			this.removedApprovedSiteCostsUpdates(listToFilter);

			this.controllerDelegate.updateControllerDelegateWithData(null);
			this.controllerDelegate.loadingStop();
		}
	}

	@Override
	public void updateWithReferesh ( Object data )
	{
		if ( data instanceof SiteCostUpdates )
		{
			SiteCostUpdates newSiteCostUpdate = (SiteCostUpdates) data;
			this.addToBeApprovedSiteCostsUpdates(newSiteCostUpdate);

			this.controllerDelegate.updateControllerDelegateWithData(null);
		}
	}

}
